package _2025_4_13;

import java.util.concurrent.TimeUnit;

public class Demo_1302 {
    public static void main(String[] args) {
        // 定义两个锁对象
        Object locker1 = new Object();
        Object locker2 = new Object();

        // 创建线程1，先获取locker1 再获取locker2
        Thread t1 = new Thread(() -> {
            System.out.println("t1申请locker1....");
            synchronized (locker1) {
                System.out.println("t1获取到了locker1");
                // 模拟业务执行时间
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 在持有locker1的基础上获取locker2
                synchronized (locker2) {
                    System.out.println("t1获取了所有的锁资源。");
                }
            }
        });

        // 创建线程2，先获取locker2 再获取locker1
        Thread t2 = new Thread(() -> {
            System.out.println("t2申请locker2....");
            synchronized (locker1) {
                System.out.println("t2 获取到了locker2.");
                // 模拟业务执行时间
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 持有locker2 的基础上获取locker1
                synchronized (locker2) {
                    System.out.println("t2获取了所有的锁资源。");
                }
            }
        });

        // 启动两个线程
        t1.start();
        t2.start();
    }
}